package net.sk.china.city.controller;

import java.util.List;

import net.sk.china.city.domain.vo.ArExchangeDetailVo;
import net.sk.china.city.domain.vo.ArExchangeVO;
import net.sk.china.city.service.IArSupermarketProductService;
import net.sk.china.city.service.IUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import net.sk.china.city.controller.base.BaseController;
import net.sk.china.city.domain.dto.AjaxResult;
import net.sk.china.city.domain.ArExchange;
import net.sk.china.city.service.IArExchangeService;
import net.sk.china.city.domain.dto.TableDataInfo;

/**
 * 兑换记录Controller
 * 
 * @author gxf
 * @date 2024-04-12
 */
@Api(tags = "兑换记录")
@RequiredArgsConstructor
@RestController
@RequestMapping("/supermarket/exchange")
public class ArExchangeController extends BaseController
{
    @Autowired
    private IArExchangeService arExchangeService;

    @Autowired
    private IUserService userService;

    @Autowired
    private IArSupermarketProductService arSupermarketProductService;


    /**
     * 查询兑换记录列表
     */
    @ApiOperation("兑换记录列表")
    @PreAuthorize("hasAnyAuthority('supermarket:exchange:list')")
    @GetMapping("/list")
    public TableDataInfo list(ArExchangeVO arExchangeVo)
    {
        startPage();
        List<ArExchangeDetailVo> list = arExchangeService.selectArExchangeList(arExchangeVo);
        return getDataTable(list);
    }

//    /**
//     * 导出兑换记录列表
//     */
//    @ApiOperation("导出兑换记录")
//    @PreAuthorize("@ss.hasPermi('supermarket:exchange:export')")
//    @Log(title = "兑换记录", businessType = BusinessType.EXPORT)
//    @PostMapping("/export")
//    public void export(HttpServletResponse response, ArExchangeVO arExchangeVo)
//    {
//        List<ArExchangeVO> list = arExchangeService.selectArExchangeList(arExchangeVo);
//        ExcelUtil<ArExchangeVO> util = new ExcelUtil<ArExchangeVO>(ArExchangeVO.class);
//        util.exportExcel(response, list, "兑换记录数据");
//    }

    /**
     * 获取兑换记录详细信息
     */
    @ApiOperation("兑换记录详情")
    @PreAuthorize("hasAnyAuthority('supermarket:exchange:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(arExchangeService.selectArExchangeById(id));
    }

    /**
     * 新增兑换记录
     */
    @ApiOperation("新增兑换记录")
    @PreAuthorize("hasAnyAuthority('supermarket:exchange:add')")
    @PostMapping
    public AjaxResult add(@RequestBody ArExchange arExchange)
    {
        return toAjax(arExchangeService.insertArExchange(arExchange));
    }

    /**
     * 修改兑换记录
     */
    @ApiOperation("修改兑换记录")
    @PreAuthorize("hasAnyAuthority('supermarket:exchange:edit')")
    @PutMapping
    public AjaxResult edit(@RequestBody ArExchange arExchange)
    {
        return toAjax(arExchangeService.updateArExchange(arExchange));
    }

    /**
     * 删除兑换记录
     */
    @ApiOperation("删除兑换记录")
    @PreAuthorize("hasAnyAuthority('supermarket:exchange:remove')")
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(arExchangeService.deleteArExchangeByIds(ids));
    }


    @ApiOperation("查询当天十条兑换记录")
    @GetMapping("/allExchange")
    public AjaxResult getAllExchange(){
        return AjaxResult.success(arExchangeService.getAllExchange());
    }

    @ApiOperation("根据手机号获取用户积分信息")
    @GetMapping("/user")
    public AjaxResult getUser(String phone){
        return AjaxResult.success(userService.getUserByPhone(phone));
    }

    @ApiOperation("根据慈善超市商品条码获取商品信息")
    @GetMapping("/supermarketProduct")
    public AjaxResult getSupermarketProduct(@RequestParam("proCode") String proCode){
        return AjaxResult.success(arSupermarketProductService.getSupermarketProductByProCode(proCode));
    }

    @ApiOperation("现场兑换")
    @PostMapping("/exchange")
    public AjaxResult exchange(@RequestBody ArExchangeDetailVo arExchangeDetailVo){
        arExchangeService.exchange(arExchangeDetailVo);
        return AjaxResult.success();
    }
}
